package com.virtualightning.mvvm

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import com.virtualightning.mvvm.utils.AppUtils
import java.lang.ref.WeakReference

/**
 *  Anchor : Create by CimZzz
 *  Time : 2021/1/25 15:40:22
 *  Project : zxs-new
 *  初始化基类
 */
object Initializer {
    ///////////////////////////////////////////////////////////////////////////
    // Add by CimZzz on 2021/1/25 下午3:41
    // Application 实例
    ///////////////////////////////////////////////////////////////////////////
    lateinit var application: Application

    ///////////////////////////////////////////////////////////////////////////
    // Add by CimZzz on 2021/3/11 上午10:21
    // 判断是否已经初始化成功
    ///////////////////////////////////////////////////////////////////////////
    private var isInitialized = false

    ///////////////////////////////////////////////////////////////////////////
    // Add by CimZzz on 2021/1/25 下午3:41
    // 用于全局初始化，必须在 Application 中调用
    ///////////////////////////////////////////////////////////////////////////
    fun init(application: Application) {
        if(isInitialized) {
            return
        }
        isInitialized = true
        Initializer.application = application
        // 注册 Activity 生命周期回调，用来监控当前 Activity
        application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(p0: Activity, p1: Bundle?) {
                AppUtils.currentActivity = WeakReference(p0)
            }

            override fun onActivityStarted(p0: Activity) {
            }

            override fun onActivityResumed(p0: Activity) {
                AppUtils.currentActivity = WeakReference(p0)
            }

            override fun onActivityPaused(p0: Activity) {
            }

            override fun onActivityStopped(p0: Activity) {
            }

            override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
            }

            override fun onActivityDestroyed(p0: Activity) {
            }
        })
    }

}